package com.example.memo1.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import android.content.Intent;

import com.example.memo1.R;
import com.example.memo1.dao.AdminDao;

public class ChangePwdActivity extends AppCompatActivity {
    private boolean isPasswordVisible = false;

    private EditText one;
    private EditText two;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changepwd);

        Toolbar toolbar = findViewById(R.id.toolbar_pwd);
        setSupportActionBar(toolbar);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed(); // 返回上一个Activity或退出应用程序
            }
        });

        Intent intent=getIntent();
        String account=intent.getStringExtra("account");

         one=findViewById(R.id.one);
        one.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(charSequence.length()>0){
                    one.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.x, 0);
                }else {
                    one.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);

                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.length()>0){
                    one.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.x, 0);
                }else {
                    one.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);

                }

            }
        });
         two=findViewById(R.id.two);
        two.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(charSequence.length()>0){
                    two.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.x, 0);
                }else {
                    two.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);

                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.length()>0){
                    two.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.x, 0);
                }else {
                    two.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);

                }

            }
        });

        Button button=findViewById(R.id.right);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String oneT=one.getText().toString().trim();
                String twoT=two.getText().toString().trim();
                if(oneT.isEmpty()){
                    Toast.makeText(ChangePwdActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else if (twoT.isEmpty()) {
                    Toast.makeText(ChangePwdActivity.this, "请输入确定密码", Toast.LENGTH_SHORT).show();
                } else if (!oneT.equals(twoT)) {
                    Toast.makeText(ChangePwdActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                }else {
                    AdminDao adminDao=new AdminDao();
                   int a= adminDao.changePassword(account,oneT);
                   if(a==0){
                       Toast.makeText(ChangePwdActivity.this, "更改失败！", Toast.LENGTH_SHORT).show();
                   }else
                   if(a>=1){
                       Toast.makeText(ChangePwdActivity.this, "修改密码成功！", Toast.LENGTH_SHORT).show();
                   }else{
                       Toast.makeText(ChangePwdActivity.this, "未知错误", Toast.LENGTH_SHORT).show();
                   }
                }
            }
        });

    }
    }


